/*
 * @Author       : 邱狮杰
 * @Date         : 2021-05-18 10:59:08
 * @LastEditTime : 2021-05-20 17:05:45
 * @FilePath     : /wedding-dress-selection/src/page/index/index.tsx
 * @Description  :
 */

import { FC, useContext } from 'react'
import { useHistory } from 'react-router-dom'
import './index.scss'
import Button from '../../components/button/index'
import FlexAuxiliarys from '../../components/layout/flexAuxiliarys/index'
import Content from './components/content/index'
import { getLocal, SynchronizationAwaitError } from '../../utils/index'
import { BaseRequest } from '../../http/index'
import { useGetOpenid } from '../../hook/index'
import { CreateUserInfo, useContextUserInfoType } from '../../reducer/index'
import Logo from '../../assets/titleLogo.png'
import { OPENID } from '../../reducer/userInfo'

type Props = {}

const Index: FC<Props> = () => {
  const { userInfoState, userInfoDispatch } = useContext(CreateUserInfo) as useContextUserInfoType
  const his = useHistory()
  useGetOpenid(() => {})
  async function Jump() {
    const send = new FormData()
    send.append('invitation_id', userInfoState.invitationId as string)
    send.append('openid', userInfoState.openId || (getLocal(OPENID) as string))
    // 绑定用户
    // BaseRequest({ url: '/invitation-bind', method: 'POST', data: send })
    const [err, res] = await SynchronizationAwaitError(BaseRequest({ url: '/invitation-bind', method: 'POST', data: send }), undefined, {
      bindError: '绑定错误',
    })
    if (!err) {
      // 没有报错
      if (res?.data.code === 200) {
        userInfoDispatch({ type: 'reset', data: { likes: res.data.data.likes } as any })
        his.push('/category')
      }
    } else {
      // 有报错
      console.log(err.message, 'err')
    }
  }

  return (
    <div className='index'>
      <img className='logo' src={Logo} alt='logo' />
      <Content />
      <FlexAuxiliarys style={{ bottom: '10%' }}>
        <Button onProxyClick={Jump} style={{ backgroundColor: 'rgba(245,245,245,0.8)', color: '#222', minWidth: '13.375rem' }} content={'开始选纱'} />
      </FlexAuxiliarys>
    </div>
  )
}

export default Index
